package com.blog.controller;

import com.blog.pojo.ArticleEs;
import com.blog.pojo.Result;
import com.blog.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.net.URLDecoder;
import java.util.List;
@CrossOrigin
@RestController
@RequestMapping("/search")
public class SearchController {
    @Autowired
    private SearchService searchService;
    @GetMapping("/deploy")
    public void deploy(){
        searchService.deploy();
    }
    @GetMapping("/search/{keyword}")
    public Result<ArticleEs> search(@PathVariable String keyword){
        try {
            String decode = URLDecoder.decode(keyword, "UTF-8");
            List<ArticleEs> articleEs = searchService.search(decode);
            return new Result<>(true,articleEs, articleEs.size());
        }catch (Exception e){
            e.printStackTrace();
            return new Result<>(false,null, 0);
        }

    }
}
